package com.zhu.wx.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhu.wx.dict.WxEventEnum;
import com.zhu.wx.entity.HandleMsg;
import com.zhu.wx.entity.RecMsg;
import com.zhu.wx.entity.ResMsg;
import com.zhu.wx.mapper.HandleMsgMapper;
import com.zhu.wx.mapper.ResMsgMapper;
import com.zhu.wx.strategy.WxRecStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 订阅消息服务类
 */
@Service
public class WxEvSubscribeService implements WxRecStrategy {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    HandleMsgMapper handleMsgMapper;
    @Autowired
    ResMsgMapper resMsgMapper;

    @Override
    public ResMsg execute(RecMsg recMsg) {
        logger.info("用户订阅-{}", recMsg.getFromUserName());
        HandleMsg handleMsg = handleMsgMapper.selectOne(Wrappers.<HandleMsg>lambdaQuery()
                .eq(HandleMsg::getEvent, WxEventEnum.SUBSCRIBE.getName()));
        if (handleMsg == null) {
            return null;
        }
        return resMsgMapper.selectById(handleMsg.getResMsgId());
    }
}
